//
//  ViewController.m
//  friend
//
//  Created by ading on 16/8/28.
//  Copyright © 2016年 阿鼎. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    int _count;
}


@property(nonatomic,strong)dispatch_source_t timer;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    self.view.backgroundColor=[UIColor whiteColor];
    
    [super viewDidLoad];
    
    [self GCDTimer];
    
//    生活就是这么美好。
   
}

-(void)GCDTimer{
    
    _count=0;
    
   
    
//   获取队列；
    
    dispatch_queue_t queue=dispatch_get_main_queue();
    
//   创建一个定时器（dispath_source_t）本质还是一个oc对象
    
    self.timer=dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_time_t start=dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0*NSEC_PER_SEC));
    
    uint64_t interval=(uint64_t)(1.0*NSEC_PER_SEC);
    
    dispatch_source_set_timer(self.timer, start, interval, 0);
    
    
//    设置回调；
    
    
    
    
    dispatch_source_set_event_handler(self.timer, ^{
        
        
        NSLog(@"......%@",[NSThread currentThread]);
        
         NSLog(@"___%d",_count);
        
        _count++;
        
       
        if (_count==4) {
            
//        取消定时器；
            
            dispatch_cancel(self.timer);
            
            self.timer=nil;
         
        }
        
    });
    
//   启动定时器；
    
   dispatch_resume(self.timer);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
